Imports Clo = Enhancements.Core.Logging
Imports Ere = Enhancements.Basic.RegularExpressions
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Pri = Enhancements.Basic.Primitives
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Val = Enhancements.Basic.Expressions.Values
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml
Imports Vbn = Microsoft.VisualBasic

Namespace Enhancements.Basic.Expressions.Functions
    ''' <summary>Represents a token that corresponds to a function call.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public NotInheritable Class FunctionCall
        Inherits Token

        ''' <exception cref="IsNullException"> </exception>
        ''' <exception cref="IsTrueNotException"> </exception>
        ''' <exception cref="IsListFilledNotException"> </exception>
        ''' <exception cref="IsInRangeNotException"> </exception>
        ''' <exception cref="IsNotException"> </exception>
        Friend Sub New(ByVal _compiler As Compiler, ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByVal _expressionEndIndex As Sys.Int32, ByVal _function As [Function], ByVal _functionSignature As FunctionSignature, ByVal _parameters() As Token)
            MyBase.New(_expression, _expressionBeginIndex, _expressionEndIndex)
            __Compiler = _compiler
            [Function] = _function
            FunctionSignature = _functionSignature
            Parameters = _parameters
        End Sub

        Private __CompilerObject As Compiler = Nothing
        Private Property __Compiler() As Compiler
            Get
                Return __CompilerObject
            End Get
            Set(ByVal _value As Compiler)
                _value.IsNullNot("__Compiler._value", True)

                __CompilerObject = _value
            End Set
        End Property

        Private __Function As [Function] = Nothing
        ''' <summary>Gets the function to call.</summary>
        Public Property [Function]() As [Function]
            Get
                Return __Function
            End Get
            Private Set(ByVal _value As [Function])
                _value.IsNullNot("Function._value", True)
                __Compiler.Functions.Contains(_value).IsTrue("__Compiler.Functions.Contains(_value)", True)

                __Function = _value
            End Set
        End Property

        Private __FunctionSignature As FunctionSignature = Nothing
        ''' <summary>Gets the function signature used in the call.</summary>
        Public Property FunctionSignature() As FunctionSignature
            Get
                Return __FunctionSignature
            End Get
            Private Set(ByVal _value As FunctionSignature)
                _value.IsNullNot("FunctionSignature._value", True)
                [Function].Signatures.Contains(_value).IsTrue("Function.Signatures.Contains(_value)", True)

                __FunctionSignature = _value
            End Set
        End Property

        Private __Parameters() As Token = {}
        ''' <summary>Gets the parameters to send to the function during the call.</summary>
        Public Property Parameters() As Token()
            Get
                Return __Parameters
            End Get
            Private Set(ByVal _value() As Token)
                _value.IsListFilled("Parameters._value", True)
                _value.Length.IsInRange("Parameters._value.Length", FunctionSignature.Parameters.Length, FunctionSignature.Parameters.Length, True)
                For _counter As Sys.Int32 = 0 To _value.Length - 1
                    Dim _isOfValidType As System.Boolean = False
                    If _value(_counter).GetType().IsDerivedOf("Parameters._value(_counter).GetType()", GetType(Val.Value), False) Then
                        Dim _valueItemCasted As Val.Value = CType(_value(_counter), Val.Value)
                        _valueItemCasted.GetType().Is("_valueItemCasted.GetType()", FunctionSignature.Parameters(_counter).Type, "FunctionSignature.Parameters(_counter).Type", True)
                        _isOfValidType = True
                    Else
                        If _value(_counter).IsOfType("Parameters._value(_counter)", GetType(Variable), False) Then
                            Dim _valueItemCasted As Variable = CType(_value(_counter), Variable)
                            _valueItemCasted.Type.IsNullNot("_valueItemCasted.Type", True)
                            _valueItemCasted.Type.Is("_valueItemCasted.Type", FunctionSignature.Parameters(_counter).Type, "FunctionSignature.Parameters(_counter).Type", True)
                            _isOfValidType = True
                        Else
                            If _value(_counter).IsOfType("Parameters._value(_counter)", GetType(FunctionCall), False) Then
                                Dim _valueItemCasted As FunctionCall = CType(_value(_counter), FunctionCall)
                                _valueItemCasted.FunctionSignature.Result.Type.Is("_valueItemCasted.FunctionSignature.Result.Type", FunctionSignature.Parameters(_counter).Type, "FunctionSignature.Parameters(_counter).Type", True)
                                _isOfValidType = True
                            End If
                        End If
                    End If
                    _isOfValidType.IsTrue("_isOfValidType", True)
                Next _counter

                __Parameters = _value
            End Set
        End Property

        ''' <summary>Returns the token's representation in the expression.</summary>
        ''' <returns> </returns>
        Public Overrides Function ToRepresentation() As System.String
            Dim _returnValue As System.String = ExpressionPart

            Return _returnValue
        End Function

    End Class
End Namespace
